/**
 * 
 */
package org.cuatrovientos.struts;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


/**
 * Main simple sample Struts2 action
 * @author Pablo Guerrero
 *
 */
public class SeeAll extends ActionSupport  
	implements ModelDriven<Car>{

	private String message;
	private Car car = new Car();
	private CarDAO carDAO = new CarDAO();
	private List<Car> cars = new ArrayList<Car>();
	
	@Override
	public Car getModel() {
		return car;
	}
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		super.validate();
		System.out.println("Main > Validate");
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		System.out.println("Main action> execute");
		message = "Main action";
		return SUCCESS;
	}
	
	/**
	 * To list all users.
	 * @return String
	 */
	public String selectAll() {
		System.out.println("Main action> Select all");
		cars = carDAO.selectAll();
		System.out.println("Main action> Select all DAO done");
		message = "Select all";
		return SUCCESS;
	}
	/**
	 * 
	 * @return String that contains the number of cars we have in database
	 */
	public String selectAllCount() {
		cars = carDAO.selectAll();
		int number = cars.size();
		String numberOfCars = Integer.toString(number);
		return numberOfCars;
	}
	
	/**
	 * handler for delete action
	 */
	public String delete () {
		System.out.println("Main action> delete");
		HttpServletRequest request = 
				(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		int id = Integer.parseInt(request.getParameter("id"));
		Car cartodelete = new Car();
		cartodelete.setId(id);
		carDAO.delete(cartodelete);
		return SUCCESS;
		
	}
	
	/**
	 * handler for saveOrUpdate action
	 * @return
	 */
	public String saveOrUpdate () {
		System.out.println("Main> SaveOrUpdate");
		carDAO.saveOrUpdate(car);
		return SUCCESS;
	}
	
	/**
	 * handler for edit action
	 * @return
	 */
	public String edit () {
		System.out.println("Main> edit");
		// get id from request. As seen on delete method
		HttpServletRequest request = 
				(HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		int id = Integer.parseInt(request.getParameter("id"));
		car = carDAO.selectById(id);
		return SUCCESS;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the car
	 */
	public Car getCar() {
		return car;
	}

	/**
	 * @param car the car to set
	 */
	public void setCar(Car car) {
		this.car = car;
	}

	/**
	 * @return the list of cars
	 */
	public List<Car> getCars() {
		return cars;
	}

	/**
	 * @param cars the list of cars to set
	 */
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}

	/**
	 * @return the carDAO
	 */
	public CarDAO getCarDAO() {
		return carDAO;
	}

	/**
	 * @param carDAO the carDAO to set
	 */
	public void setCarDAO(CarDAO carDAO) {
		this.carDAO = carDAO;
	}


	
}
